package com.yearEndWorkload.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.mapper.OutstandingChildMapper;
import com.yearEndWorkload.pojo.entity.OutstandingChild;
import com.yearEndWorkload.pojo.entity.OutstandingPerformance;
import com.yearEndWorkload.pojo.excel.exports.NewOutstandingExportExcel;
import com.yearEndWorkload.pojo.form.add.AddOutstandingPerformanceChildForm;
import com.yearEndWorkload.pojo.form.add.AddOutstandingPerformanceForm;
import com.yearEndWorkload.pojo.form.update.UpdateOutstandingPerformanceChildForm;
import com.yearEndWorkload.pojo.form.update.UpdateOutstandingPerformanceForm;
import com.yearEndWorkload.pojo.query.OutstandingQuery;
import com.yearEndWorkload.pojo.vo.BaseOutstandingTreeVo;
import com.yearEndWorkload.pojo.vo.BaseVo;
import com.yearEndWorkload.pojo.vo.OutstandingChildVo;
import com.yearEndWorkload.pojo.vo.OutstandingPerformanceVo;
import com.yearEndWorkload.service.OutstandingPerformanceService;
import com.yearEndWorkload.mapper.OutstandingPerformanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author admin
* @description 针对表【outstanding_performance(新增突出业绩成果奖励管理)】的数据库操作Service实现
* @createDate 2024-03-06 22:46:30
*/
@Service
public class OutstandingPerformanceServiceImpl extends ServiceImpl<OutstandingPerformanceMapper, OutstandingPerformance>
    implements OutstandingPerformanceService{

    @Autowired
    OutstandingPerformanceMapper outstandingPerformanceMapper;

    @Autowired
    OutstandingChildMapper outstandingChildMapper;

    @Override
    public Result getList(OutstandingQuery outstandingQuery) {
        Page<OutstandingPerformanceVo> page = new Page<>(outstandingQuery.getCurrentPage(),outstandingQuery.getPageSize());
        page = outstandingPerformanceMapper.getList(page,outstandingQuery);

        List<OutstandingPerformanceVo> recordsR = page.getRecords();

        BaseVo<BaseOutstandingTreeVo> criteriaBaseVo = new BaseVo<>();

        criteriaBaseVo.setCurrentPage(outstandingQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(outstandingQuery.getPageSize());
        criteriaBaseVo.setAllPages(page.getPages());
        criteriaBaseVo.setAllRecordNum(page.getTotal());

        List<BaseOutstandingTreeVo> records= new ArrayList<>();

        for (int i = 0; i < recordsR.size(); i++) {
            List<OutstandingChildVo> childVoList= outstandingChildMapper.selectListByProjectId(recordsR.get(i).getId(),outstandingQuery);

            BaseOutstandingTreeVo baseOutstandingTreeVo = new BaseOutstandingTreeVo();
            baseOutstandingTreeVo.setOutstandingPerformanceVo(recordsR.get(i));
            baseOutstandingTreeVo.setChildVos(childVoList);
            records.add(baseOutstandingTreeVo);
            if(childVoList.size()==0){
                recordsR.get(i).setFlagAllocation("未分配");
            }else {
                recordsR.get(i).setFlagAllocation("已分配");
            }
        }
        criteriaBaseVo.setRecords(records);

        return Result.success(criteriaBaseVo);
    }

    @Override
    public Result addOutstandingPerformance(AddOutstandingPerformanceForm addOutstandingPerformanceForm) {
        OutstandingPerformance outstandingPerformance = new OutstandingPerformance();
        BeanUtil.copyProperties(addOutstandingPerformanceForm,outstandingPerformance);
        outstandingPerformanceMapper.insert(outstandingPerformance);
        return Result.success("添加成功");
    }

    @Override
    public Result updateOutstandingPerformance(UpdateOutstandingPerformanceForm updateOutstandingPerformanceForm) {
        OutstandingPerformance outstandingPerformance = new OutstandingPerformance();
        BeanUtil.copyProperties(updateOutstandingPerformanceForm,outstandingPerformance);
        outstandingPerformanceMapper.updateById(outstandingPerformance);
        return Result.success("修改成功");
    }

    @Override
    public Result deleteOutstandingPerformanceByIds(List<String> list) {
        outstandingPerformanceMapper.deleteBatchIds(list);
        return Result.success("删除成功");
    }

    @Override
    public Result addOutstandingPerformanceChild(AddOutstandingPerformanceChildForm addOutstandingPerformanceChildForm) {
        OutstandingChild outstandingChild = new OutstandingChild();
        BeanUtil.copyProperties(addOutstandingPerformanceChildForm,outstandingChild);
        outstandingChildMapper.insert(outstandingChild);
        return  Result.success("添加成功");
    }

    @Override
    public Result updateOutstandingPerformanceChild(UpdateOutstandingPerformanceChildForm updateOutstandingPerformanceForm) {
        OutstandingChild outstandingChild = new OutstandingChild();
        BeanUtil.copyProperties(updateOutstandingPerformanceForm,outstandingChild);
        outstandingChildMapper.updateById(outstandingChild);
        return Result.success("修改成功");
    }

    @Override
    public Result deleteOutstandingPerformanceChildByIds(List<String> list) {
        outstandingChildMapper.deleteBatchIds(list);
        return Result.success("删除成功");
    }

    @Override
    public List<NewOutstandingExportExcel> getOutstandingExportExcels() {
        List<NewOutstandingExportExcel> re = new ArrayList<>();
        List<NewOutstandingExportExcel> list = outstandingPerformanceMapper.getNewOutstandingExcel();

        //构造
        for (NewOutstandingExportExcel newOutstandingExportExcel : list) {
            re.add(newOutstandingExportExcel);
            Integer projectId = newOutstandingExportExcel.getId();
            List<NewOutstandingExportExcel> temp = outstandingChildMapper.getNewOutstandingExcelByProjectId(projectId);
            if (temp.size() > 0) {
                re.addAll(temp);
            }

        }

        return re;
    }

}




